# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@io_bazel_rules_docker//container:image.bzl", "container_image")
load("@io_bazel_rules_docker//container:layer.bzl", "container_layer")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_k8s//k8s:object.bzl", "k8s_object")
load("//bazel:pl_build_system.bzl", "pl_go_binary", "pl_go_image")

go_library(
    name = "artifact_db_updater_lib",
    srcs = ["main.go"],
    importpath = "px.dev/pixie/src/utils/artifacts/artifact_db_updater",
    visibility = ["//visibility:private"],
    deps = [
        "//src/cloud/artifact_tracker/schema",
        "//src/cloud/shared/pgmigrate",
        "//src/shared/artifacts/versionspb:versions_pl_go_proto",
        "//src/shared/artifacts/versionspb/utils",
        "//src/shared/services/pg",
        "@com_github_gogo_protobuf//jsonpb",
        "@com_github_gogo_protobuf//types",
        "@com_github_golang_migrate_migrate//source/go_bindata",
        "@com_github_jmoiron_sqlx//:sqlx",
        "@com_github_sirupsen_logrus//:logrus",
        "@com_github_spf13_pflag//:pflag",
        "@com_github_spf13_viper//:viper",
    ],
)

pl_go_binary(
    name = "artifact_db_updater",
    embed = [":artifact_db_updater_lib"],
    visibility = ["//src:__subpackages__"],
)

container_layer(
    name = "changelogs",
    directory = "/changelogs",
    files = [
        "VERSIONS.json",
    ],
)

container_image(
    name = "changelogs_image",
    # We need a debug image because the artifact_db_updater_job calls busybox to set a TRAP.
    base = "//:pl_cc_base_debug_image",
    layers = [":changelogs"],
)

pl_go_image(
    name = "artifact_db_updater_image",
    base = ":changelogs_image",
    binary = ":artifact_db_updater",
)

k8s_object(
    name = "artifact_db_updater_job",
    images = {"gcr.io/pixie-oss/pixie-dev/utils/artifact_db_updater_image:latest": ":artifact_db_updater_image"},
    kind = "Job",
    tags = ["manual"],
    template = ":artifact_db_updater_job.yaml",
)
